package com.itheima.常用API.基本类型包装类;

public class 冒泡排序 {
    public static void main(String[] args) {
        int[] arr = {9, 98, 88, 8, 87, 7, 76, 6, 65, 5, 54, 4, 43, 3, 32, 2, 21, 1};
        System.out.println("排序前数组:   ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println("排序后数组:   ");
        arraysSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static void arraysSort(int[] arr) {
        for (int j = 0; j < arr.length - 1; j++) {
            for (int i = 0; i < arr.length - 1 - j; i++) {
                int replace;
                if (arr[i] > arr[i + 1]) {
                    replace = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = replace;
                }
            }
        }
    }

    public static void arraysSort2(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int replace,replace1;
            if (arr[i] > arr[i + 1]) {
                replace = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = replace;
            }
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[i]) {
                    replace1 = arr[i];
                    arr[i] = arr[j];
                    arr[j] = replace1;
                    break;
                }
            }
        }
    }
}


